package com.liankuan.console.service.StrategyOtherServiceImpl;

import com.liankuan.console.common.ResultEnum;
import com.liankuan.console.controller.vo.StrategyListVO;
import com.liankuan.console.controller.vo.StrategyOtherVO.StrategyInfoVO;
import com.liankuan.console.controller.vo.StrategyVO;
import com.liankuan.console.controller.vo.request.Strategy.ExchangeIO;
import com.liankuan.console.controller.vo.request.Strategy.SaveStrategyIO;
import com.liankuan.console.controller.vo.request.*;
import com.liankuan.console.dto.Exchange;
import com.liankuan.console.dto.Strategy;
import com.liankuan.console.dto.StrategyExchange;
import com.liankuan.console.exception.ServiceException;
import com.liankuan.console.repository.ExchangeRepository;
import com.liankuan.console.repository.StrategyExchangeRepository;
import com.liankuan.console.repository.StrategyRepository;
import com.liankuan.console.service.StrategyService;
import com.liankuan.console.util.Config;
import com.liankuan.console.util.JarStateUtil;
import com.liankuan.console.util.ReadFileUtil;
import com.liankuan.console.util.StrategyShell;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StrategyOtherServiceImpl {
    private final StrategyRepository strategyRepository;
    private final ExchangeRepository exchangeRepository;
    private final StrategyExchangeRepository strategyExchangeRepository;

    @Autowired
    public StrategyOtherServiceImpl(StrategyRepository strategyRepository, ExchangeRepository exchangeRepository,
                                    StrategyExchangeRepository strategyExchangeRepository) {
        this.strategyRepository = strategyRepository;
        this.exchangeRepository = exchangeRepository;
        this.strategyExchangeRepository = strategyExchangeRepository;
    }

    /**
     * 转为策略端的获取策略信息
     *
     * @param id
     * @return
     */
    public StrategyInfoVO findOne(int id) {
        //获取策略的基本信息
        Strategy strategy = strategyRepository.findById(id);

        if (strategy == null) {
            throw new ServiceException(ResultEnum.NOT_STRATEGY);
        }

        //获取策略对应的所有的交易所ID信息
        List<StrategyExchange> strategyExchanges = strategyExchangeRepository.findAllByStrategyId(strategy.getId());

        //通过exchange的id获取策略对应的所有交易所的基本信息
        List<Exchange> exchanges = new ArrayList<>();
        strategyExchanges.forEach(record -> {
            Exchange exchange = exchangeRepository.findById(record.getExchangeId());
            exchanges.add(exchange);
        });
        return new StrategyInfoVO(strategy, strategyExchanges, exchanges);
    }

}
